#!/bin/sh

HOME_DB_FOLDER="$HOME/.cache"
DB_FILE="locate.db"

if [ "$1" = "-h" ]; then
	cat <<EOF>&2
Usage: ${0##*/} [FOLDERS]

Update the 'locate' databases for the home folder, external drives and the FOLDERS arguments.

For external drives, databases are only updated if found, they are not
automatically created (unless explicitly specified in the FOLDERS arguments).

- The database for the home folder is stored in '$HOME_DB_FOLDER/$DB_FILE'.
- Other databases are stored in '$DB_FILE' at the target root.

EOF
	exit
fi

update() {
	[ $# -ne 2 ] && set -- "$1" "$1/locate.db"
	echo >&2 "Updating '$2' database for '$1'..."
	## From https://git.archlinux.org/svntogit/packages.git/tree/trunk/updatedb.conf?h=packages/mlocate.
	updatedb -l 0 -o "$2" -U "$1" \
		--prune-bind-mounts=1 \
		--prunefs="9p afs anon_inodefs auto autofs bdev binfmt_misc cgroup cifs coda configfs cpuset cramfs debugfs devpts devtmpfs ecryptfs exofs ftpfs fuse fuse.encfs fuse.sshfs fusectl gfs gfs2 hugetlbfs inotifyfs iso9660 jffs2 lustre mqueue ncpfs nfs nfs4 nfsd pipefs proc ramfs rootfs rpc_pipefs securityfs selinuxfs sfs shfs smbfs sockfs sshfs sysfs tmpfs ubifs udf usbfs vboxsf" \
		--prunepaths="/gnu/store /afs /mnt /net /sfs /tmp /udev /var/cache /var/lib/pacman/local /var/lock /var/run /var/spool /var/tmp" \
		--prunenames=".git .hg .svn .cache Trash .Trash-$(id -u) .snapshots"
}

update "$HOME" "$HOME_DB_FOLDER/$DB_FILE"

# Lookup $1's root and direct subfolders (useful when it's a Btrfs partition with subvolumes).
find_db() {
	if [ -f "$1/$DB_FILE" ]; then
		update "$1"
	else
		for i in "$1"/*; do
			if [ -f "$i/$DB_FILE" ]; then
				update "$i"
			fi
		done
	fi
}

## Only update external media databases:
for i in "/run/media/$USER"/* "/media/$USER"/*; do
	find_db "$i"
done

## Only update non-external media:
for i in "/media"/*; do
	[ "$i" = "/media/$USER" ] && continue
	find_db "$i"
done

## Create database for the specified folders.
for i; do
	[ -d "$i" ] && update "$i"
done
